﻿using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
using System.Web;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Core;
using Furion.DatabaseAccessor;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Yitter.IdGenerator;
using Microsoft.AspNetCore.SignalR;

using Admin.NET.Core;
using Base.Rest;

namespace Admin.NET.Application;
/// <summary>
/// 教师服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class TeacherService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Teacher> _rep;
    private readonly IConfigService _configService;
    private readonly ITest _testService;
    

    public TeacherService(SqlSugarRepository<Teacher> rep, IConfigService configService, ITest testService
        )
    {
        _rep = rep;
        _configService = configService;
        _testService = testService;
    }

    /// <summary>
    /// 分页查询教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("分页查询教师")]
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    //[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)]
    public async Task<SqlSugarPagedList<TeacherOutput>> Page(TeacherInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.Xuhao.Contains(input.SearchKey.Trim())
                || u.Code.Contains(input.SearchKey.Trim())
                || u.Name.Contains(input.SearchKey.Trim())
                || u.Tel.Contains(input.SearchKey.Trim())
                || u.UserName.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.Xuhao), u => u.Xuhao.Contains(input.Xuhao.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.UserName), u => u.UserName.Contains(input.UserName.Trim()))
            .Select<TeacherOutput>();
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("增加教师")]
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddTeacherInput input)
    {
        var entity = input.Adapt<Teacher>();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("删除教师")]
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteTeacherInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        //await _rep.FakeDeleteAsync(entity);   //假删除
        await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("更新教师")]
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateTeacherInput input)
    {
        var entity = input.Adapt<Teacher>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("获取教师")]
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<Teacher> Detail([FromQuery] QueryByIdTeacherInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取教师列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("获取教师列表")]
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<TeacherOutput>> List([FromQuery] TeacherInput input)
    {
        return await _rep.AsQueryable().Select<TeacherOutput>().ToListAsync();
    }

    /// <summary>
    /// test
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("Test")]
    [HttpGet]
    [AllowAnonymous]
    [ApiDescriptionSettings(Name = "Test")]
    public async Task<string> GetTest()
    {
        var response = await _configService.GetTokenExpire();
        return "hello world " + response.Result.ToString();
    }
    /// <summary>
    /// test
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("Test1")]
    [HttpGet]
    [AllowAnonymous]
    [ApiDescriptionSettings(Name = "Test1")]
    public async Task<string> GetTest1()
    {
        var response = await _testService.GetWeatherForecast();
        var str = "";
        foreach (var item in response)
        {
            str += item.Date.ToString() + " " + item.TemperatureC + " " + item.Summary + "\n";
        }
        return "hello test1 "+ response.Count() + str;
    }
    
}

